import pytest
from core.rest_client import RestClient
from utils.log_util import logger
from utils.yaml_util import YamlUtil


@pytest.fixture(scope='session', autouse=False)
def access_token():
    """
    获取 access_token，结合 fixture 用于session级别的初始化
    :return: access_token，dict
    """
    access_token_param = YamlUtil.read_testcase_yaml('access_token.yaml')['access_token']
    response = RestClient.do_request(url='/cgi-bin/gettoken', method='GET', params=access_token_param)
    if response['access_token']:
        access_token_value = response['access_token']
        access_token = {"access_token": access_token_value}
        return access_token
    else:
        logger.error(f"请求返回无access_token，请检查接口入参或者根据返回排查，参考企业微信官方")


@pytest.fixture(scope='function', autouse=True)
def setup_teardown():
    logger.info(f"<<<测试用例开始执行>>>")
    yield
    logger.info(f"<<<测试用例执行结束>>>\n\n")


if __name__ == '__main__':
    access_token()
